﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Marbles.Core.Connectors;

namespace Marbles.Core.BBS
{
    public class ForumCollection : ICollection<Forum>
    {

        public int Count
        {
            get { return items.Count; }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public Forum this[int index] { get { return items[index]; } set { items[index] = value; } }
        public string SiteId { get { return _siteid; } set { _siteid = value; } }
        public Dictionary<string, string> Categories { get { return _categorys; } }

        private Dictionary<string, string> _categorys = null;
        private List<Forum> items = null;
        private string _siteid = string.Empty;

        public ForumCollection()
        {
            _categorys = new Dictionary<string, string>();
            items = new List<Forum>();
        }

        public ForumCollection(String siteid)
            : this()
        {
            _siteid = siteid;
            Load();
        }

        public void Load()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT a.*, b.title, b.postby, b.postdate, c.title AS categorytitle FROM forumheader a LEFT JOIN forumitems b ON a.siteid = b.siteid AND a.forumname = b.forumname AND a.lastpostid = b.postid INNER JOIN forumcategory c ON a.siteid = c.siteid AND a.category = c.category WHERE a.siteid = @siteid ORDER BY c.title ASC, a.title ASC");
            cmd.AddParameter("@siteid", _siteid);
            Dictionary<string, string> categoryitems = new Dictionary<string, string>();
            DataTable dt = dbc.FetchDataTable(cmd);
            Clear();

            foreach (DataRow dr in dt.Rows)
            {
                if (!categoryitems.ContainsKey(dr["category"].ToString()))
                {
                    categoryitems.Add(dr["category"].ToString(), dr["categorytitle"].ToString());
                }

                items.Add(new Forum(dr));
            }

            foreach (string key in (from k in categoryitems.Keys orderby categoryitems[k] ascending select k))
            {
                _categorys.Add(key, categoryitems[key]);
            }

        }

        public Forum[] GetForumByCategory(string category)
        {
            List<Forum> list = new List<Forum>();
            foreach (Forum f in items)
            {
                if (f.Category == category) { list.Add(f); }
            }
            return list.ToArray();
        }

        public void Add(Forum item)
        {
            items.Add(item);
        }

        public static Dictionary<string, string> GetForumCategories(string SiteId)
        {
            Dictionary<string, string> values = new Dictionary<string, string>();
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT category, title FROM forumcategory WHERE siteid = @siteid ORDER BY title ASC");
            cmd.AddParameter("@siteid", SiteId);
            foreach (DataRow dr in dbc.FetchDataTable(cmd).Rows)
            {
                values.Add(dr["category"].ToString(), dr["title"].ToString());
            }
            return values;
        }

        public void Clear()
        {
            items.Clear();
            _categorys.Clear();
        }

        public bool Contains(Forum item)
        {
            return items.Contains(item);
        }

        public void CopyTo(Forum[] array, int arrayIndex)
        {
            items.CopyTo(array, arrayIndex);
        }

        public bool Remove(Forum item)
        {
            return items.Remove(item);
        }

        public IEnumerator<Forum> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return items.GetEnumerator();
        }
    }
}
